package com.tgjorgoski.utils;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ColorToolbar extends JToolBar {
	public ColorToolbar() {
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		setLayout(gbl);
		
		setFloatable(false);
		setRollover(true);
		
		// reusable listener for each button        
		ActionListener color_listener = new ActionListener() { 
			public void actionPerformed(ActionEvent evt) {
				if (evt.getSource() instanceof ColorButton) {
					ColorButton cb = (ColorButton)evt.getSource();
					
					//HACK:Remove the rollover border of the button 
					cb.setEnabled(false);
					cb.setEnabled(true);
					
					selectColor(cb.getTheColor());
				}
			}
		};

		// set up the standard 12 colors
		Color[] colors = new Color[12];
		String[] names = new String[12];
		colors[0] = Color.black;
		names[0] = "Black";
		colors[1] = Color.blue;
		names[1] = "Blue";
		colors[2] = Color.cyan;
		names[2] = "Cyan";
		colors[3] = Color.gray;
		names[3] = "Gray";
		colors[4] = Color.green;
		names[4] = "Green";
		colors[5] = Color.red.darker().darker();
		names[5] = "Dark Red";
		colors[6] = Color.magenta;
		names[6] = "Magenta";
		colors[7] = Color.orange;
		names[7] = "Orange";
		colors[8] = Color.pink;
		names[8] = "Pink";
		colors[9] = Color.red;
		names[9] = "Red";
		colors[10] = Color.yellow;
		names[10] = "Yellow";
		colors[11] = Color.green.darker().darker().darker();
		names[11] = "Dark Green";

		// lay out the grid
		c.gridheight = 1;
		c.gridwidth = 1;
		c.fill = c.NONE;
		c.weightx = 0;
		c.weighty = 0;
		for(int i=0; i<3; i++) {
			for(int j=0; j<4; j++) {
			c.gridx=j;
			c.gridy=i;
			JButton button = new ColorButton(colors[j+i*4], names[j+i*4], 12, 12);
			gbl.setConstraints(button,c);
			add(button);
			button.addActionListener(color_listener);
			}
		}
		
	}
	// fire off a selectedColor property event
	public static final String COLOR_PROPERTY = "selectedColor";
	public void selectColor(Color newColor) {
		firePropertyChange(COLOR_PROPERTY,null, newColor);
	}

}